home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus 2002 #3 / Amiga Plus CD - 2002 - No. 03.iso / AmigaPlus / AmigaOS / Aplus_Dev / AP-Website / forum / wbboard / admin / Upload.class.php < prev   
Encoding:
PHP Script  |  2002-10-28  |  7.0 KB  |  284 lines

  1. <?php
  2. if (!function_exists("is_uploaded_file") and get_cfg_var("safe_mode")==0) {
  3.     function is_uploaded_file($filename) {
  4.             if (!$tmp_file = get_cfg_var('upload_tmp_dir')) $tmp_file = dirname(tempnam('', ''));
  5.             $tmp_file .= '/' . basename($filename);
  6.             return (ereg_replace('/+', '/', $tmp_file) == $filename);
  7.       } 
  8.     function move_uploaded_file($filename, $destination) {
  9.              if (is_uploaded_file($filename))  {
  10.                    if (copy($filename,$destination)) return true;
  11.                    else return false;
  12.                }
  13.               else return false;
  14.        }
  15. }
  16.  
  17. if (!defined('UPLOAD_MAX_FILE_SIZE')) {
  18.    define('UPLOAD_MAX_FILE_SIZE', $conf[avatar_size]); // 1MB = 1048576
  19. }
  20. if (!defined('UPLOAD_IMAGE_MAX_WIDTH')) {
  21.     define('UPLOAD_IMAGE_MAX_WIDTH', $conf[avatar_width]);
  22. }
  23. if (!defined('UPLOAD_IMAGE_MAX_HEIGHT')) {
  24.     define('UPLOAD_IMAGE_MAX_HEIGHT', $conf[avatar_height]);
  25. }
  26. if (!defined('UPLOAD_FIELD_NAME')) {
  27.     define('UPLOAD_FIELD_NAME', "uploadFile");
  28. }
  29.  
  30. class Upload
  31. {
  32.  
  33.     
  34.     
  35.     // array
  36.     var $uploadErrors;
  37.     var $registeredMimeTypes;
  38.     var $allowedMimeTypes;
  39.     
  40.     // int
  41.     var $maxImageWidth;
  42.     var $maxImageHeight;
  43.     var $maxFileSize;
  44.     var $insertid;
  45.     var $fieldCounter;
  46.     
  47.     // string
  48.     var $uploadPath;
  49.     var $uploadFieldName;
  50.     var $fieldName;
  51.     var $errorType;
  52.     var $file_extension;
  53.     
  54.     // bool
  55.     var $imageSizeOk;
  56.     var $uploadValidated;
  57.     var $uploadFail;
  58.     
  59.     function Upload() {
  60.         $this->uploadErrors         = array();
  61.         $this->registeredMimeTypes     = array();
  62.         $this->allowedMimeTypes     = array();
  63.         
  64.         $this->maxImageWidth        = 0;
  65.         $this->maxImageHeight        = 0;
  66.         $this->maxFileSize            = 0;
  67.         $this->fieldCounter            = 0;
  68.         
  69.         $this->uploadFieldName         = "";
  70.         $this->uploadPath            = "";
  71.         
  72.         $this->imageSizeOk            = false;
  73.         $this->uploadValidated        = false;
  74.         $this->uploadFail            = false;
  75.         
  76.         
  77.         if(!$this->maxImageWidth || !$this->maxImageHeight) {
  78.             $this->setMaxImageSize();
  79.         }
  80.         
  81.         if(!$this->maxFileSize) {
  82.             $this->setMaxFileSize();
  83.         }
  84.         
  85.     }
  86.     
  87.     function setMaxImageSize($maxImageWidth = UPLOAD_IMAGE_MAX_WIDTH, $maxImageHeight = UPLOAD_IMAGE_MAX_HEIGHT)
  88.     {
  89.         $this->maxImageWidth     = $maxImageWidth;
  90.         $this->maxImageHeight     = $maxImageHeight;
  91.     }
  92.     
  93.     function setUploadPath($uploadPath)
  94.     {
  95.         $this->uploadPath     = $uploadPath;
  96.     }
  97.     
  98.     function setAllowedMimeTypes($allowedMimeTypes = array())
  99.     {
  100.         $this->allowedMimeTypes = $allowedMimeTypes;
  101.     }
  102.     
  103.     function setMaxFileSize($maxFileSize = UPLOAD_MAX_FILE_SIZE)
  104.     {
  105.         $this->maxFileSize = $maxFileSize;
  106.     }
  107.     
  108.     function getUploadErrors()
  109.     {
  110.         return $this->uploadErrors; // array
  111.     }
  112.     
  113.     function setError($errorType)
  114.     {
  115.         $this->uploadErrors[$this->HTTP_POST_FILES[$this->uploadFieldName]['name']][] = $errorType; //+++
  116.     }
  117.  
  118.     function getAllowedMimeTypes()
  119.     {
  120.         return $this->allowedMimeTypes;
  121.     }
  122.     
  123.     function getUploadImageSize()
  124.     {
  125.         $dimensions = @GetImageSize($this->uploadFile); 
  126.         return array($dimensions[0],$dimensions[1]);
  127.     }
  128.     
  129.     function checkMimeType()
  130.     {
  131.         if (!in_array($this->file_extension,$this->getAllowedMimeTypes())) {
  132.             $this->setError("mimeException");
  133.             return false;
  134.         } else return true;
  135.     }
  136.     
  137.     function checkImageSize() {
  138.         $this->imageSize = $this->getUploadImageSize($this->uploadFile); 
  139.         
  140.         $imageSizeOK = true;
  141.         
  142.         if ($this->imageSize[0] > $this->maxImageWidth) {
  143.             $imageSizeOK = false;
  144.             $this->setError("imageWidthException");
  145.         }
  146.  
  147.         if ($this->imageSize[1] > $this->maxImageHeight) {
  148.             $imageSizeOK= false;
  149.             $this->setError("imageHeightException");
  150.         }
  151.         return $imageSizeOK;
  152.     }
  153.     
  154.     function copyFile() 
  155.     {
  156.         $ok = move_uploaded_file($this->uploadFile, $this->uploadPath . "/avatar-".$this->insertid.".".$this->file_extension); //+++
  157.         @chmod( $this->uploadPath . "/avatar-".$this->insertid.".".$this->file_extension, 0777);
  158.         return $ok;
  159.     }
  160.     
  161.     function checkMaxFileSize()
  162.     {
  163.         if ($this->HTTP_POST_FILES[$this->uploadFieldName]['size'] > $this->maxFileSize) {         //+++ ISSUE
  164.             return false;
  165.         } else {
  166.             return true;
  167.         }
  168.     }
  169.     
  170.     function setRegisteredMimeTypes($registeredMimeTypes = array())
  171.     {
  172.         if (sizeof($registeredMimeTypes) == 0) {
  173.             $this->registeredMimeTypes = 
  174.                 array(
  175.                     "application/x-gzip-compressed"     => ".tar.gz, .tgz",
  176.                     "application/x-zip-compressed"         => ".zip",
  177.                     "application/x-tar"                    => ".tar",
  178.                     "text/plain"                        => ".php, .txt, .inc (etc)",
  179.                     "text/html"                            => ".html, .htm (etc)",
  180.                     "image/bmp"                         => ".bmp, .ico",
  181.                     "image/gif"                         => ".gif",
  182.                     "image/pjpeg"                        => ".jpg, .jpeg",
  183.                     "image/jpeg"                        => ".jpg, .jpeg",
  184.                     "image/x-png"                        => ".png",
  185.                     "audio/mpeg"                        => ".mp3 etc",
  186.                     "audio/wav"                            => ".wav",
  187.                     "application/pdf"                    => ".pdf",
  188.                     "application/x-shockwave-flash"     => ".swf",
  189.                     "application/msword"                => ".doc",
  190.                     "application/vnd.ms-excel"            => ".xls",
  191.                     "application/octet-stream"            => ".exe, .fla, .psd (etc)"
  192.                 );
  193.         } else {
  194.             $this->registeredMimeTypes = $registeredMimeTypes;
  195.         }
  196.     }
  197.     
  198.     function setDefaults()
  199.     {
  200.         if(!$this->registeredMimeTypes) {
  201.             $this->setRegisteredMimeTypes();
  202.         }
  203.         
  204.         if(!$this->maxImageWidth || !$this->maxImageHeight) {
  205.             $this->setMaxImageSize();
  206.         }
  207.         
  208.         if(!$this->maxFileSize) {
  209.             $this->setMaxFileSize();
  210.         }
  211.     }
  212.     
  213.     
  214.     function processUpload() { 
  215.         
  216.         $this->uploadFile = $this->HTTP_POST_FILES[$this->uploadFieldName]['tmp_name'];
  217.         $this->setDefaults();
  218.  
  219.         if ($this->uploadFile == "none") {
  220.             $this->setError("noFileException");
  221.             $this->uploadFail = true;
  222.         }
  223.         
  224.         if (!$this->checkMaxFileSize()) {
  225.             $this->setError("fileTooBigException");
  226.             $this->uploadFail = true;
  227.         } elseif(in_array("cannotGetImageSize",$this->getUploadErrors())) {
  228.             $this->uploadFail = true;
  229.         }
  230.         
  231.         if (!$this->uploadFail) {
  232.             if (ereg("image",$this->HTTP_POST_FILES[$this->uploadFieldName]['type'])) {
  233.                 $this->imageSizeOk = $this->checkImageSize();
  234.             } else {
  235.                 $this->imageSizeOk = true;
  236.             }
  237.         }
  238.         
  239.         $this->file_extension = strtolower(substr(strrchr($this->HTTP_POST_FILES[$this->uploadFieldName]['name'],"."),1));
  240.           $this->file_name = substr($this->HTTP_POST_FILES[$this->uploadFieldName]['name'], 0, (intval(strlen($this->file_extension))+1)*-1);
  241.   
  242.         if($this->checkMimeType() && $this->imageSizeOk && !$this->uploadFail) {
  243.             global $db_zugriff, $n, $groupid, $posts, $setuserid, $insertid;
  244.             
  245.             $db_zugriff->query("INSERT INTO bb".$n."_avatars VALUES ('','".$this->file_name."','".$this->file_extension."','$groupid','$posts','$setuserid')");
  246.             $this->insertid = $db_zugriff->insert_id();
  247.             $insertid = $this->insertid;
  248.             if (!$this->copyFile()) {
  249.                 $db_zugriff->query("DELETE FROM bb".$n."_avatars WHERE id = '$this->insertid'");
  250.                 $this->setError("fileCopyException");
  251.             }
  252.         }
  253.         
  254.         if (sizeof($this->uploadErrors) == 0)
  255.         {
  256.             $this->uploadValidated = true;
  257.         }
  258.         return $this->uploadValidated;
  259.     }
  260.     
  261.     function doUpload() {
  262.     
  263.         $this->HTTP_POST_FILES     = $GLOBALS['HTTP_POST_FILES'];     //array
  264.         $this->fieldCounter     = $GLOBALS['fieldCounter'];    //int
  265.  
  266.         for ($i=0; $i<$this->fieldCounter; $i++) {
  267.         
  268.             $this->uploadFieldName    = $GLOBALS['uploadFileName'][$i];
  269.             $currentUpload = $this->processUpload();
  270.  
  271.             if (!$currentUpload) {
  272.                 $errorsOccured = true;
  273.             }
  274.         }
  275.         
  276.         if ($errorsOccured) {
  277.             return false;
  278.         } else {
  279.             return true;
  280.         }
  281.     }
  282. }
  283. ?>
  284.